package packager;

import java.io.FileWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class Main {

    private void log(String s) {
        System.out.println("\t" + s);
    }
    private void log1(String s) {
        System.out.println("\t\t" + s);
    }
    private void log2(String s) {
        System.out.println("\t\t\t" + s);
    }

    private void writeFile(FileWriter file, String s) throws Exception {
        file.write(s);
    }
    private void writeFile(RandomAccessFile file, byte[] b) throws Exception {
        file.write(b);
    }
    private void writeFile(RandomAccessFile file, int i) throws Exception {
        file.writeInt(i);
    }
    private void writeFile(RandomAccessFile file, String s) throws Exception {
        file.writeBytes(s);
    }


    private void parseFile(String filename) {
        String[] worksheetsNames = null;
        String[] resourcesNames = null;
        List[] stringId = null;
        List[] stringFile = null;

        int worksheetsCant = 0;
        int actualWorksheet = 0;

        try {
            RandomAccessFile file = new RandomAccessFile(filename, "r");

            while(file.getFilePointer() < file.length()) {
                String line = file.readLine();
                if(line.indexOf("Worksheet ss") > 0) {
                    worksheetsCant++;
                }
            }
            log("worksheetsCant: " + worksheetsCant);
            worksheetsNames = new String[worksheetsCant];
            resourcesNames = new String[worksheetsCant];
            stringFile = new List[worksheetsCant];
            stringId = new List[worksheetsCant];

            file.seek(0);
            String line = file.readLine();
            while(actualWorksheet < worksheetsCant) {
                if(line.indexOf("Worksheet ss") > 0) {
                    worksheetsNames[actualWorksheet] = line.substring(line.indexOf("\"")+1, line.lastIndexOf("\""));
                    stringId[actualWorksheet] = new ArrayList();
                    stringFile[actualWorksheet] = new ArrayList();
                    log("parse worksheet " + worksheetsNames[actualWorksheet]);

                    while(line.indexOf("</Table>") < 0) {
                        if(line.indexOf("<Row>") > 0) {
                            line = file.readLine();
                            while(line.indexOf("</Row>") < 0) {
                                if(line.indexOf(":name:") > 0) {
                                    line = file.readLine();
                                    resourcesNames[actualWorksheet] = line.substring(line.indexOf("\">")+2, line.indexOf("</Data>"));
                                    log1("parse resource " + resourcesNames[actualWorksheet]);
                                    line = file.readLine();
                                }
                                else if(line.indexOf("<Cell>") >= 0) {
                                    String id = worksheetsNames[actualWorksheet] + "_" + line.substring(line.indexOf("\">")+2, line.lastIndexOf("</D"));
                                    log2(id);
                                    stringId[actualWorksheet].add(id);
                                    line = file.readLine();
                                    String res = line.substring(line.indexOf("\">")+2, line.lastIndexOf("</D"));
                                    log2(res);
                                    stringFile[actualWorksheet].add(res);
                                    //System.out.println("2-" + line);
                                }
                                line = file.readLine();
                                //System.out.println("1-" + line);
                            }
                        }
                        line = file.readLine();
                        //System.out.println("0-" + line);
                    }

                    while(line.indexOf("</Worksheet>") < 0) {
                        line = file.readLine();
                    }
                    actualWorksheet++;
                }
                line = file.readLine();
            }
            file.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        try {
            FileWriter file = null;
            log("creating Data.java");
            for(int i=0; i<stringId.length; i++) {
                log1("creating interface " + worksheetsNames[i]);
                file = new FileWriter("Data.java", true);
                writeFile(file, "\ninterface " + worksheetsNames[i]);
                writeFile(file, "\n{");
                writeFile(file, "\n\tpublic static String resName = \"/" + resourcesNames[i] + "\";");
                for(int w=0; w<stringId[i].size(); w++) {
                    writeFile(file, "\n\tpublic static int " + stringId[i].get(w) + " = " + w + ";");
                }
                writeFile(file, "\n}\n\n");
                file.close();
            }
            RandomAccessFile binary_file;
            RandomAccessFile tmp_file;
            byte[][] array = null;
            for(int w=0; w<worksheetsNames.length; w++) {
                binary_file = new RandomAccessFile( resourcesNames[w].toString(), "rw");
                array = new byte[stringFile[w].size()][];
                int off = 4 + (array.length<<2);
                writeFile(binary_file, array.length);
                for(int t=0; t<stringFile[w].size(); t++) {
                    log2(stringFile[w].get(t).toString());
                    tmp_file = new RandomAccessFile(stringFile[w].get(t).toString(), "rw");
                    array[t] = new byte[(int)tmp_file.length()];
                    tmp_file.read(array[t]);
                    writeFile(binary_file, off);
                    off += (int)tmp_file.length();
                    log2(""+off);
                    tmp_file.close();
                }
                for(int t=0; t<array.length; t++) {
                    writeFile(binary_file, array[t]);
                }
                binary_file.close();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String file = "";
        if(args.length == 2 && args[0].equals("-f")) {
            file = args[1];
            System.out.println("parse file " + file);
        }
        else {
            System.out.println("Parameters error.");
            System.out.println("use: java Packager -f file");
            return;
        }
        new Main().parseFile(file);
    }
}
